---
--类名: GatherControlView.lua
--创建日期: 2015-7-9
--创建人: 陈小虎
--功能描述: 采集中控制视图
--修改内容：
--修改日期:   
--修改人:     
--修改备注: 
--
local MoLayer = require("app.widget.MoLayer")

local GatherControlView = class("GatherControlView",function()
    return MoLayer.new(true)--display.newLayer("GatherControlView")
end)

function GatherControlView:ctor(controller)
	self.mController = controller

	-- self:addTouchMask()

	self.mUINode = cc.uiloader:load(Res.GatherControlUi)
	-- self.mUINode:setPosition(cc.p(display.width - display.width / 10,display.height / 2))
    self.mUINode:setPosition(cc.p(0,0))
    self:addChild(self.mUINode)

    self:hideLoadingSprite()

    self:addListener()
end

-- function GatherControlView:addTouchMask()
--     self.mTouchMaskListener = cc.EventListenerTouchOneByOne:create()
--     self.mTouchMaskListener:setSwallowTouches(true)
--     self.mTouchMaskListener:registerScriptHandler(function(touch, event)
--         return true
--     end, cc.Handler.EVENT_TOUCH_BEGAN)

--     self.mTouchMaskListener:registerScriptHandler(function(touch, event)

--         end, cc.Handler.EVENT_TOUCH_ENDED)
--     self:getEventDispatcher():addEventListenerWithSceneGraphPriority(self.mTouchMaskListener, self)
    
-- end

function GatherControlView:addListener()
	local stopButton = self.mUINode:getChildByName("StopButton")
	local function touchEvent_Stop(sender,eventType)
        if eventType == ccui.TouchEventType.ended then
        	DoEventHandler.setStopGatherAction(true,self.mController:getGatherNpcId())
        	--UIHandler:hideGatherControl()
        end
    end

    stopButton:addTouchEventListener(touchEvent_Stop)
end

function GatherControlView:setGatherTime(time)
    self.mGatherTime = time
    self:refreshLoading()
end

function GatherControlView:setGatherOverCallback(callback)
    self.mGatherCallback = callback
end

function GatherControlView:setProgress(curTimes,maxTimes)
    local numTextNode = self.mUINode:getChildByName("num")
    if curTimes == nil and maxTimes == nil then
        numTextNode:setVisible(false)
    else
        numTextNode:setVisible(false)
        -- numTextNode:setVisible(true)
        -- numTextNode:setString(curTimes .. "/" .. maxTimes)
    end
    
end

function GatherControlView:hideLoadingSprite()
    local loadingSprite = self.mUINode:getChildByName("Loading")
    loadingSprite:setVisible(false)
end

function GatherControlView:refreshLoading()
    if self.mProgressTimer then
        self.mUINode:removeChild(self.mProgressTimer)
        self.mProgressTimer = nil
        if self.mGatherCallback then
            self.mGatherCallback()
        end
    end
    local loadingSprite = self.mUINode:getChildByName("Loading")
    loadingSprite:setVisible(false)

    local to = cc.ProgressTo:create(self.mGatherTime, 100)-- / 0.75

    local left = cc.ProgressTimer:create(loadingSprite)
    left:setType(cc.PROGRESS_TIMER_TYPE_RADIAL)
    left:setMidpoint(cc.p(0.5,0.5))
    left:setAnchorPoint(loadingSprite:getAnchorPoint())
    left:setPosition(cc.p(loadingSprite:getPosition()))
    left:setScale(loadingSprite:getScale())
    self.mUINode:addChild(left)

    local function call()
        self.mUINode:removeChild(left)
        if self.mGatherCallback then
            self.mGatherCallback()
        end
    end
    local action = cc.Sequence:create(to,cc.CallFunc:create(call))
    left:runAction(action)
    self.mProgressTimer = left
end

return GatherControlView